#include "Game.h"
extern Vector<BaseObject*> g_listObjects;
// should be used to set default values like setting pointers to NULL
Game::Game()
{
	
	//m_eState = CONSOLE_SELECT;
	m_eState = PIECE_SELECT;

	//debuggin
	//m_eState=PLAYER_SWITCH;
	//m_eState=PLAYER_SWITCH;
	//m_eState=CARD_SELECT;
	m_eState=CARD_SELECT;
	 whoseTurnIsIt=0;
}

bool Game::Init()
{
	// this is where the game setup should happen
	// things such as dynamically allocated memory should go here
	m_players[YBOX].Init(YBOX);
	m_players[FS3].Init(FS3);
	m_players[PC].Init(PC);
	m_players[WEE].Init(WEE);

	m_pBoard = new Board;
	m_pBoard->Init();
	
	m_bDrawBoard = true;

	m_pBoard->scale.x=4.0;
	m_pBoard->scale.y=4.0;
	m_pBoard->scale.z=4.0;

	m_pBoard->pos.z=0.0;
	m_pBoard->pos.y=-1.0;
	m_pBoard->pos.x=0.0;


	m_pBoard->rotate.z=0.0;
	m_pBoard->rotate.y=0.0;
	m_pBoard->rotate.x=0.0;
	for(int i=0;i<g_inumPieces;++i)
	{
		g_listObjects.push(m_players[YBOX].m_pieceInfo[i]);
		g_listObjects.push(m_players[FS3].m_pieceInfo[i]);
		g_listObjects.push(m_players[PC].m_pieceInfo[i]);	
		g_listObjects.push(m_players[WEE].m_pieceInfo[i]);	
	}
	
	g_listObjects.push(m_pBoard);

	mi_currentId=0;
	return true;	
}

bool Game::Update()
{ 
	switch(m_eState)
	{
	case CONSOLE_SELECT:
		ConsoleSelect();
		break;
	case ORDER_SELECT:
		OrderSelect();
		break;
	case CARD_SELECT:
		CardSelect();
		break;
	case PIECE_SELECT:
		PieceSelect();
		break;
	case PIECE_MOVE:
		PieceMove();
		break;
	case PLAYER_SWITCH:
		PlayerSwitch();
		break;
	case END:
		EndGame();
		break;
	case PAUSE:
		break;
	}

	// DEBUGGING ONLY
	if(DInput::Engine()->keyHeld(DIK_LCONTROL))
	{
		if(DInput::Engine()->keyPressed(DIK_W))
			m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.x -= 0.5f;
		if(DInput::Engine()->keyPressed(DIK_S))
			m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.x += 0.5f;
		if(DInput::Engine()->keyPressed(DIK_A))
			m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.z -= 0.5f;
		if(DInput::Engine()->keyPressed(DIK_D))
			m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.z += 0.5f;
	}
	else
	{
		if(DInput::Engine()->keyPressed(DIK_A))
		{
			mi_currentId++;
			m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos=m_pBoard->m_boardSpots[mi_currentId].pos;
			
		}
		if(DInput::Engine()->keyPressed(DIK_S))
		{
			mi_currentId--;
			m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos= m_pBoard->m_boardSpots[mi_currentId].pos;
			
		}
	//	if(DInput::Engine()->keyHeld(DIK_A))
	//		m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.z -= 0.5f;
	//	if(DInput::Engine()->keyHeld(DIK_D))
	//		m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.z += 0.5f;
	}

	return true;
}

void Game::ConsoleSelect()
{
}

void Game::OrderSelect()
{
}

void Game::CardSelect()
{
	m_bDrawBoard=false;
	
}

void Game::PieceSelect()
{

}

void Game::PieceMove()
{
	//mi_currentId++;
		//	m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos=m_pBoard->m_boardSpots[mi_currentId].pos;
}
void Game::PlayerSwitch()
{
	int piecesAtHome = 0;

	// counts up the amount of pieces at home for each player, if any player has all four, change game state to end
	for(int i = 0; i < g_inumPlayers; i++)
	{
		for(int j = 0; j < g_inumPieces; j++)
		{
			
			if(m_players[i].m_pieceInfo[j]->m_home)
				piecesAtHome++;
		}

		if(piecesAtHome == g_inumPieces)
		{
			m_eState = END;
			break;
		}
		piecesAtHome = 0;
	}
		
	m_bDrawBoard=false;
	m_eState=PLAYER_SWITCH;
	if(DInput::Engine()->keyPressed(DIK_SPACE))
	{
		//go to the next player
		Game::Engine()->whoseTurnIsIt++;
		if(Game::Engine()->whoseTurnIsIt>=g_inumPlayers)
			Game::Engine()->whoseTurnIsIt=0;
		Game::Engine()->m_bDrawBoard=true;
		Game::Engine()->m_eState=CARD_SELECT;
	}
}

void Game::EndGame()
{
	PostQuitMessage(0);
}

void Game::Release()
{

}

Game::~Game()
{

}